﻿using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using Dimebrain.TweetSharp.Model.Twitter.Converters;
using Newtonsoft.Json;

namespace Dimebrain.TweetSharp.Model.Twitter
{
#if !SILVERLIGHT
    [Serializable]
#endif
#if !Smartphone
    [DataContract]
    [DebuggerDisplay("{Name}:'{Query}'")]
#endif
    [JsonObject(MemberSerialization.OptIn)]
    public class TwitterSavedSearch : PropertyChangedBase,
                                 IComparable<TwitterSavedSearch>,
                                 IEquatable<TwitterSavedSearch>,
                                 ITwitterEntity
    {
        private long _id;
        private string _name;
        private string _query;
        private string _position;
        private DateTime _createdDate;

        [JsonProperty("id")]
#if !Smartphone
        [DataMember]
#endif
        public long Id
        {
            get { return _id; }
            set
            {
                if (_id == value)
                {
                    return;
                }

                _id = value;
                OnPropertyChanged("Id");
            }
        }

        [JsonProperty("name")]
#if !Smartphone
        [DataMember]
#endif
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name == value)
                {
                    return;
                }

                _name = value;
                OnPropertyChanged("Name");
            }
        }

        [JsonProperty("query")]
#if !Smartphone
        [DataMember]
#endif
        public string Query
        {
            get { return _query; }
            set
            {
                if (_query == value)
                {
                    return;
                }

                _query = value;
                OnPropertyChanged("Query");
            }
        }

        [JsonProperty("position")]
#if !Smartphone
        [DataMember]
#endif
        public string Position
        {
            get { return _position; }
            set
            {
                if (_position == value)
                {
                    return;
                }

                _position = value;
                OnPropertyChanged("Position");
            }
        }

        [JsonProperty("created_at")]
        [JsonConverter(typeof(TwitterDateTimeConverter))]
#if !Smartphone
        [DataMember]
#endif
        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set
            {
                if (_createdDate == value)
                {
                    return;
                }

                _createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        public int CompareTo(TwitterSavedSearch other)
        {
            return other.Id == Id ? 0 : other.Id > Id ? -1 : 1;
        }

        public bool Equals(TwitterSavedSearch savedSearch)
        {
            if (ReferenceEquals(null, savedSearch))
            {
                return false;
            }
            if (ReferenceEquals(this, savedSearch))
            {
                return true;
            }
            return savedSearch.Id == Id;
        }

        public override bool Equals(object status)
        {
            if (ReferenceEquals(null, status))
            {
                return false;
            }
            if (ReferenceEquals(this, status))
            {
                return true;
            }
            return status.GetType() == typeof(TwitterSavedSearch) && 
                Equals((TwitterSavedSearch)status);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(TwitterSavedSearch left, TwitterSavedSearch right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(TwitterSavedSearch left, TwitterSavedSearch right)
        {
            return !Equals(left, right);
        }
    }
}
